#!/bin/bash

# 数据备份脚本
BACKUP_DIR="./scripts/backup"
DATE=$(date +%Y%m%d_%H%M%S)

echo "开始数据备份..."

# 创建备份目录
mkdir -p ${BACKUP_DIR}/${DATE}

# 备份 MongoDB
echo "备份 MongoDB..."
docker exec devops-mongodb mongodump --out /tmp/backup
docker cp devops-mongodb:/tmp/backup ${BACKUP_DIR}/${DATE}/mongodb

# 备份 Prometheus 数据
echo "备份 Prometheus 数据..."
docker cp devops-prometheus:/prometheus ${BACKUP_DIR}/${DATE}/prometheus

# 压缩备份
echo "压缩备份文件..."
cd ${BACKUP_DIR}
tar -czf ${DATE}.tar.gz ${DATE}
rm -rf ${DATE}

# 清理旧备份（保留最近7天）
find ${BACKUP_DIR} -name "*.tar.gz" -mtime +7 -delete

echo "备份完成: ${BACKUP_DIR}/${DATE}.tar.gz"
